package com.suiwei.base.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author suiwei
 * @date 2022-10-25 12:12
 */
public class DateUtils {
    public static String DATE_TO_STRING_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_TO_STRING_yyyy_MM_dd = "yyyy-MM-dd";
    private static SimpleDateFormat simpleDateFormat;

    /**
     * 指定日志往后推num天，负数是往前
     * @param date
     * @param num
     * @return
     */
    public static  String nextDate(String date,int num ){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(stringToDate(date,DATE_TO_STRING_yyyy_MM_dd));
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + num);
        return DateToString(calendar.getTime(),DATE_TO_STRING_yyyy_MM_dd);
    }
    public static  String getTime(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat(DATE_TO_STRING_yyyy_MM_dd_HH_mm_ss);
        return  dateFormat.format(date);

    }
    public static  String getDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat(DATE_TO_STRING_yyyy_MM_dd);
        return  dateFormat.format(date);
    }

    /**
     * 返回两个日期之间的所有日期
     * @param beginTime
     * @param endTime
     * @return
     */

    public static List<String> getDatesBetween(String beginTime, String endTime) {
        List<String> allDate = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = null;
        Date dEnd = null;
        try {
            dBegin = sdf.parse(beginTime);
            dEnd = sdf.parse(endTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        allDate.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            allDate.add(sdf.format(calBegin.getTime()));
        }
        return allDate;
    }


    public static String DateToString(Date source, String pattern) {

        simpleDateFormat = new SimpleDateFormat(pattern);

        return simpleDateFormat.format(source);

    }

    public static Date stringToDate(String source,String pattern) {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        try{
            simpleDateFormat = new SimpleDateFormat(pattern);
            date = simpleDateFormat.parse(source);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }


}
